package com.example.administrator.shihuo.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;

import com.example.administrator.shihuo.Config.NetConfig;
import com.example.administrator.shihuo.Modle.ShaiWuModle;
import com.example.administrator.shihuo.R;
import com.example.administrator.shihuo.activities.NoScrollListView;
import com.example.administrator.shihuo.adapters.ShaiWuPullToReshAdapter;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sunshine on 2016/5/10.
 */
public class ShareCollectionsFragment extends Fragment {

    private View rootView;
    private Button allBtn;
    private Button essenceBtn;
    private NoScrollListView mListView;
    private List<ShaiWuModle.DataBean.ListBean> listBeans;
    private ShaiWuPullToReshAdapter adapter;
    private boolean isToBottom;
    private int Page = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.sharecollections_fragment_layout, container, false);
        initView();
        intData();
        setData();
        setListener();
        return rootView;
    }

    private void initView() {
        allBtn = (Button) rootView.findViewById(R.id.btn_sharecollectionsactivity_all);
        essenceBtn = (Button) rootView.findViewById(R.id.btn_sharecollectionsactivity_essence);
        mListView = (NoScrollListView) rootView.findViewById(R.id.pull_sharecollectionsactivity_show);
    }

    private void intData() {
        listBeans = new ArrayList<>();
        adapter = new ShaiWuPullToReshAdapter(getActivity(), listBeans);
        allBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBeans.clear();
                getRequest(String.format(NetConfig.ALL_PATH, 1));
            }
        });
        essenceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBeans.clear();
                getRequest(String.format(NetConfig.ESSENCE_PATH, 1));
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void setData() {
        mListView.setAdapter(adapter);
    }

    private void setListener() {
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.e("1542", "=-=-=-========================---------------" + scrollState);
                if (scrollState == SCROLL_STATE_IDLE && isToBottom == true) {
                    Page++;
                    getRequest(String.format(NetConfig.ALL_PATH, Page));
                    Log.e("1542", "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" + String.format(NetConfig.ALL_PATH, Page));
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    isToBottom = true;
                    Log.e("1542", "--------------------------aa" + isToBottom);
                } else {
                    isToBottom = false;
                    Log.e("1542", "==================================================" + isToBottom);
                }
            }
        });

    }

    private void getRequest(String path) {
        //下载json数据；
        try {
            OkHttpUtils.get().url(path).tag("get").build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                }

                @Override
                public void onResponse(String response) {
                    //解析；
                    Gson gson = new Gson();
                    ShaiWuModle shaiwuModle = gson.fromJson(response, ShaiWuModle.class);
                    listBeans.addAll(shaiwuModle.getData().getList());
                    //刷新；
                    adapter.notifyDataSetChanged();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
